#install.packages(“rgdal”) #install.packages(“pryr”) #install.packages(“rgeos”) #install.packages(“htmlwidgets”) #install.packages(“leaflet”)

Create a list of the shapefiles inlcuded in the data folder

The correct UK layer is UK_2011_404.shp

# create a directory where the shapefiles are
dir <- paste0(getwd(),"/data_to_use")
file_list <- list.files(dir, pattern="*.shp$", full.names=TRUE)
file_list
##  [1] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/AUT_2010_99.shp"   
##  [2] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/BEL_2005_589_2.shp"
##  [3] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/BRL_2011_118.shp"  
##  [4] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/BUL_2011_263.shp"  
##  [5] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/CHE_2000_184.shp"  
##  [6] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/CZE_2010_77.shp"   
##  [7] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/DNK_2011_99_2.shp" 
##  [8] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/EST_2016_223.shp"  
##  [9] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/FIN_2011_336.shp"  
## [10] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/GER_2009_412.shp"  
## [11] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/GRE_2011_54.shp"   
## [12] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/HUN_2010_168.shp"  
## [13] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/ITA_2009_107.shp"  
## [14] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/LTU_2010_58.shp"   
## [15] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/NLD_2010_431_2.shp"
## [16] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/NOR_2013_428.shp"  
## [17] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/POL_2010_376_2.shp"
## [18] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/POR_2011_30.shp"   
## [19] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/ROU_2002_42.shp"   
## [20] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/RUS_2010_80_2.shp" 
## [21] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/SPA_2011_52.shp"   
## [22] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/SWE_2012_290.shp"  
## [23] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/TUR_2012_81.shp"   
## [24] "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use/UK_2011_404.shp"

Read in the shapefiles for each country separately

library(rgdal)
## Loading required package: sp
## rgdal: version: 1.4-6, (SVN revision 841)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.4.2, released 2019/06/28
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/gdal
##  GDAL binary built with GEOS: FALSE 
##  Loaded PROJ.4 runtime: Rel. 5.2.0, September 15th, 2018, [PJ_VERSION: 520]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/proj
##  Linking to sp version: 1.3-1
AUT <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[1])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "AUT_2010_99"
## with 99 features
## It has 7 fields
BEL <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[2])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "BEL_2005_589_2"
## with 589 features
## It has 8 fields
## Integer64 fields read as strings:  9_3_pop
BRL <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[3])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "BRL_2011_118"
## with 118 features
## It has 20 fields
## Integer64 fields read as strings:  1_pop
BUL <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[4])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "BUL_2011_263"
## with 263 features
## It has 15 fields
## Integer64 fields read as strings:  ID_0 ID_1 ID_2
CHE <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[5])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "CHE_2000_184"
## with 184 features
## It has 13 fields
## Integer64 fields read as strings:  2_pop
CZE <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[6])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "CZE_2010_77"
## with 77 features
## It has 7 fields
DNK <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[7])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "DNK_2011_99_2"
## with 99 features
## It has 7 fields
## Integer64 fields read as strings:  4_pop
EST <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[8])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "EST_2016_223"
## with 223 features
## It has 16 fields
## Integer64 fields read as strings:  ID_0 ID_1 ID_2 7_2v_pop
FIN <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[9])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "FIN_2011_336"
## with 336 features
## It has 11 fields
## Integer64 fields read as strings:  7_pop
GER <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[10])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "GER_2009_412"
## with 412 features
## It has 9 fields
GRE <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[11])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "GRE_2011_54"
## with 54 features
## It has 7 fields
HUN <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[12])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "HUN_2010_168"
## with 168 features
## It has 14 fields
## Integer64 fields read as strings:  ID_0 ID_1 ID_2
ITA <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[13])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "ITA_2009_107"
## with 107 features
## It has 7 fields
LTU <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[14])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "LTU_2010_58"
## with 48 features
## It has 16 fields
## Integer64 fields read as strings:  ID_0 ID_1 ID_2 6_2v_pop
NLD <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[15])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "NLD_2010_431_2"
## with 431 features
## It has 13 fields
## Integer64 fields read as strings:  AANT_INW OPP_TOT OPP_LAND OPP_WATER 8_pop
NOR <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[16])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "NOR_2013_428"
## with 428 features
## It has 15 fields
## Integer64 fields read as strings:  5_2_pop
POL <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[17])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "POL_2010_376_2"
## with 376 features
## It has 16 fields
## Integer64 fields read as strings:  ID_0 ID_1 ID_2
POR <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[18])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "POR_2011_30"
## with 30 features
## It has 11 fields
ROU <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[19])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "ROU_2002_42"
## with 42 features
## It has 10 fields
RUS <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[20])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "RUS_2010_80_2"
## with 80 features
## It has 8 fields
SPA <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[21])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "SPA_2011_52"
## with 52 features
## It has 8 fields
SWE <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[22])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "SWE_2012_290"
## with 290 features
## It has 9 fields
## Integer64 fields read as strings:  1_2_pop
TUR <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[23])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "TUR_2012_81"
## with 81 features
## It has 7 fields
UK <- readOGR(dsn = "data_to_use", 
                layer = tools::file_path_sans_ext(basename(file_list[24])))
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_to_use", layer: "UK_2011_404"
## with 404 features
## It has 12 fields
## Integer64 fields read as strings:  CodeID uk_pop

Pre-process the data by each country so we keep the name of the region as well as the net migration and z scores

For the countries that they have names for two levels I used the level 2 which I believe corresponds to regions that we map

# Print the names of the columns
str(AUT@data)
## 'data.frame':    99 obs. of  7 variables:
##  $ name       : Factor w/ 99 levels "Amstetten","Baden",..: 12 11 24 33 52 59 60 61 67 15 ...
##  $ CodeID     : num  1 3 4 5 6 7 8 9 2 19 ...
##  $ X3_2_pop   : num  12850 40624 26448 17483 38976 ...
##  $ X3_2_nmr   : num  0.599 0.633 -0.31 0.143 0.423 ...
##  $ X3_2_ln_pop: num  2.48 1.96 1.74 1.85 2.24 ...
##  $ X3_2_countr: Factor w/ 1 level "Austria": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X3_2_z_nmr : num  1.602 1.679 -0.507 0.544 1.194 ...
# Select the columns that we are interested in
AUT@data <- AUT@data[,c("name", "X3_2_nmr", "X3_2_z_nmr")]

# Rename the columns so there are consistent across countries

names(AUT@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(BEL@data)
## 'data.frame':    589 obs. of  8 variables:
##  $ name       : Factor w/ 589 levels "Aalst","Aalter",..: 18 4 67 69 73 82 83 141 152 220 ...
##  $ CodeSTR    : num  11002 11001 11004 11005 11007 ...
##  $ Code       : int  2 1 3 4 5 6 7 8 9 10 ...
##  $ X9_3_nmr   : num  -0.889 0.621 0.606 1.401 -1.07 ...
##  $ X9_3_pop   : Factor w/ 584 levels "10010","10018",..: 388 121 67 148 10 354 278 232 164 574 ...
##  $ X9_3_ln_pop: num  3.35 3.1 2.77 3.34 3.41 ...
##  $ X9_3_countr: Factor w/ 1 level "Belgium": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X9_3_z_nmr : num  -1.659 0.465 0.443 1.561 -1.913 ...
# Select the columns that we are interested in
BEL@data <- BEL@data[,c("name", "X9_3_nmr", "X9_3_z_nmr")]

# Rename the columns so there are consistent across countries

names(BEL@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(BRL@data)
## 'data.frame':    118 obs. of  20 variables:
##  $ GID_0      : Factor w/ 1 level "BLR": 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_0     : Factor w/ 1 level "Belarus": 1 1 1 1 1 1 1 1 1 1 ...
##  $ GID_1      : Factor w/ 6 levels "BLR.1_1","BLR.2_1",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_1     : Factor w/ 6 levels "Brest","Homyel'",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ NL_NAME_1  : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
##  $ GID_2      : Factor w/ 118 levels "BLR.1.1_1","BLR.1.10_1",..: 1 9 10 11 12 13 14 15 16 2 ...
##  $ NAME_2     : Factor w/ 118 levels "Aktsyabar","Arsha",..: 7 10 13 26 31 38 39 42 53 61 ...
##  $ VARNAME_2  : Factor w/ 118 levels "Baranovichy",..: 1 4 11 22 26 33 34 37 44 59 ...
##  $ NL_NAME_2  : Factor w/ 17 levels "Bykhov","Mosty",..: NA NA NA NA NA NA NA NA NA NA ...
##  $ TYPE_2     : Factor w/ 1 level "Raion": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ENGTYPE_2  : Factor w/ 1 level "District": 1 1 1 1 1 1 1 1 1 1 ...
##  $ CC_2       : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
##  $ HASC_2     : Factor w/ 117 levels "BY.BR.BA","BY.BR.BR",..: 1 3 2 4 5 6 7 9 8 11 ...
##  $ BLR_area   : num  2258 1413 1690 1855 1710 ...
##  $ X1_pop     : Factor w/ 118 levels "10047","102859",..: 40 108 83 93 70 95 104 84 116 112 ...
##  $ X1_nmr     : num  0.336 -0.533 0.936 -1.242 -0.631 ...
##  $ X1_ln_popde: num  4.76 1.67 4.79 1.35 1.25 ...
##  $ X1_bsu_name: Factor w/ 118 levels "Baranovichy",..: 1 4 11 22 26 33 34 37 44 59 ...
##  $ X1_l_area  : Factor w/ 1 level "2257.89": 1 NA NA NA NA NA NA NA NA NA ...
##  $ X1_z_nmr   : num  1.2467 0.1409 1.7234 -0.7134 0.0229 ...
# Select the columns that we are interested in
BRL@data <- BRL@data[,c("NAME_2", "X1_nmr", "X1_z_nmr")]

# Rename the columns so there are consistent across countries

names(BRL@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(BUL@data)
## 'data.frame':    263 obs. of  15 variables:
##  $ ID_0       : Factor w/ 1 level "37": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ISO        : Factor w/ 1 level "BGR": 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_0     : Factor w/ 1 level "Bulgaria": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_1       : Factor w/ 28 levels "1","10","11",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_1     : Factor w/ 29 levels "Blagoevgrad",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_2       : Factor w/ 263 levels "1","10","100",..: 1 112 187 198 209 220 231 242 253 2 ...
##  $ NAME_2     : Factor w/ 262 levels "Aksakovo","Alfatar",..: 12 15 20 72 79 83 113 165 181 193 ...
##  $ TYPE_2     : Factor w/ 2 levels "City","Obshtina": 2 2 1 2 2 2 2 2 2 2 ...
##  $ ENGTYPE_2  : Factor w/ 2 levels "City","Municipality": 2 2 1 2 2 2 2 2 2 2 ...
##  $ NL_NAME_2  : Factor w/ 189 levels "Айтос","Аксаково",..: 10 13 18 46 43 173 84 121 133 141 ...
##  $ VARNAME_2  : Factor w/ 56 levels "Accadânalar|Akkadınlar",..: NA NA 19 NA 18 NA NA NA NA 47 ...
##  $ X5_pop     : num  13005 9503 76859 14528 31915 ...
##  $ X5_nmr     : num  -1.18 -0.78 -1.49 -1.18 -0.71 -0.71 -0.52 -0.81 -0.22 -0.35 ...
##  $ X5_ln_popde: num  1.44 1.51 2.09 1.57 1.99 1.5 1.23 1.94 1.69 1.63 ...
##  $ X5_z_nmr   : num  -1.324 -0.726 -1.787 -1.324 -0.621 ...
# Select the columns that we are interested in
BUL@data <- BUL@data[,c("NAME_2", "X5_nmr", "X5_z_nmr")]

# Rename the columns so there are consistent across countries

names(BUL@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(CHE@data)
## 'data.frame':    184 obs. of  13 variables:
##  $ BEZIRK     : int  1801 2303 1405 1903 213 503 1814 1401 900 215 ...
##  $ KT         : int  18 23 14 19 2 5 18 14 9 2 ...
##  $ NAME       : Factor w/ 184 levels "Amt Aarberg",..: 33 139 103 41 14 59 114 82 184 154 ...
##  $ District   : num  1801 2303 1405 1903 213 ...
##  $ Code       : num  102 164 82 118 25 47 115 78 54 27 ...
##  $ PAR        : num  8064 10793 4473 54775 12890 ...
##  $ X2_pop     : Factor w/ 184 levels "103702","10414",..: 169 5 131 145 20 49 165 121 170 142 ...
##  $ X2_nmr     : num  -5.32 -1.455 -1.878 0.783 0.566 ...
##  $ X2_ln_popde: num  1.05 1.23 2.17 2.67 2.16 ...
##  $ X2_bsu_name: Factor w/ 184 levels "Amt Aarberg",..: 33 139 103 41 14 59 114 82 184 154 ...
##  $ X2_country : Factor w/ 1 level "Switzerland": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X2_l_area  : num  724.2 633.8 30.2 117.8 88.2 ...
##  $ X2_z_nmr   : num  -1.792 -0.345 -0.503 0.493 0.412 ...
# Select the columns that we are interested in
CHE@data <- CHE@data[,c("NAME", "X2_nmr", "X2_z_nmr")]

# Rename the columns so there are consistent across countries

names(CHE@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(CZE@data)
## 'data.frame':    77 obs. of  7 variables:
##  $ Name       : Factor w/ 77 levels "Beneov","Beroun",..: 8 9 26 57 51 65 68 4 3 6 ...
##  $ Code       : num  14 15 16 17 18 19 20 59 56 58 ...
##  $ X6_3_nmr   : num  0.3381 -0.2886 -0.0215 0.1359 -0.3514 ...
##  $ X6_3_pop   : num  187240 61671 93223 70632 51511 ...
##  $ X6_3_ln_pop: num  2.06 1.61 1.68 1.76 1.61 ...
##  $ X6_3_countr: Factor w/ 1 level "Czech Republic": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X6_3_z_nmr : num  0.465 -0.645 -0.172 0.107 -0.757 ...
# Select the columns that we are interested in
CZE@data <- CZE@data[,c("Name", "X6_3_nmr", "X6_3_z_nmr")]

# Rename the columns so there are consistent across countries

names(CZE@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(DNK@data)
## 'data.frame':    99 obs. of  7 variables:
##  $ name       : Factor w/ 99 levels "Aabenraa","Aalborg",..: 4 5 6 7 8 64 9 10 11 14 ...
##  $ CodeID     : num  5 18 48 6 58 53 29 7 89 3 ...
##  $ X4_pop     : Factor w/ 99 levels "105","107618",..: 24 19 50 64 21 27 49 34 36 5 ...
##  $ X4_nmr     : num  -0.206 -0.133 -0.512 0.196 -0.514 ...
##  $ X4_ln_popde: num  3.04 2.55 1.91 3.17 1.69 ...
##  $ X4_country : Factor w/ 1 level "Denmark": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X4_z_nmr   : num  -0.1462 -0.0675 -0.4771 0.2875 -0.4791 ...
# Select the columns that we are interested in
DNK@data <- DNK@data[,c("name", "X4_nmr", "X4_z_nmr")]

# Rename the columns so there are consistent across countries

names(DNK@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(EST@data)
## 'data.frame':    223 obs. of  16 variables:
##  $ ID_0       : Factor w/ 1 level "73": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ISO        : Factor w/ 1 level "EST": 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_0     : Factor w/ 1 level "Estonia": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_1       : Factor w/ 16 levels "1","10","11",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_1     : Factor w/ 16 levels "Harju","Hiiu",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_2       : Factor w/ 223 levels "1","10","100",..: 1 112 147 158 169 180 191 202 213 2 ...
##  $ NAME_2     : Factor w/ 222 levels "Abja","Aegviidu",..: 2 8 24 32 73 53 54 57 72 77 ...
##  $ TYPE_2     : Factor w/ 3 levels "Linn","Vald",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ ENGTYPE_2  : Factor w/ 3 levels "Parish","Town",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ NL_NAME_2  : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
##  $ VARNAME_2  : Factor w/ 1 level "Lake Võrtsjärv": NA NA NA NA NA NA NA NA NA NA ...
##  $ X7_2v_pop  : Factor w/ 195 levels "10000","1005",..: 171 149 37 154 51 137 86 135 148 161 ...
##  $ X7_2v_nmr  : num  -1.039 -1.325 6.328 0.553 -2.089 ...
##  $ X7_2v_ln_po: num  1.811 1.058 1.929 1.478 0.728 ...
##  $ X7_2v_count: Factor w/ 1 level "Estonia": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X7_2v_z_nmr: num  -0.169 -0.272 2.477 0.403 -0.546 ...
# Select the columns that we are interested in
EST@data <- EST@data[,c("NAME_2", "X7_2v_nmr", "X7_2v_z_nmr")]

# Rename the columns so there are consistent across countries

names(EST@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(FIN@data)
## 'data.frame':    336 obs. of  11 variables:
##  $ Name       : Factor w/ 336 levels "Akaa","Alajrvi",..: 280 157 9 25 146 16 64 160 330 282 ...
##  $ CodeSTR    : Factor w/ 336 levels "005","009","010",..: 275 157 9 25 147 18 61 160 327 277 ...
##  $ Code       : num  277 156 9 25 145 18 61 159 327 279 ...
##  $ CodeInt    : int  277 156 9 25 145 18 61 159 327 279 ...
##  $ X7_pop     : Factor w/ 332 levels "1004","101948",..: 14 47 327 44 69 250 200 193 207 3 ...
##  $ X7_nmr     : num  -9.91 -0.161 0.938 -0.659 1.796 ...
##  $ X7_ln_popde: num  -0.49 0.949 0.104 0.092 0.278 ...
##  $ X7_bsu_name: Factor w/ 336 levels "Akaa","Alaj�rvi",..: 277 156 9 25 145 18 61 159 328 279 ...
##  $ X7_country : Factor w/ 1 level "Finland": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X7_l_area  : num  343 1750 755 1227 968 ...
##  $ X7_z_nmr   : num  -9.885 0.22 1.358 -0.297 2.248 ...
# Select the columns that we are interested in
FIN@data <- FIN@data[,c("Name", "X7_nmr", "X7_z_nmr")]

# Rename the columns so there are consistent across countries

names(FIN@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(GER@data)
## 'data.frame':    412 obs. of  9 variables:
##  $ Name       : Factor w/ 412 levels "Ahrweiler","Aichach-Friedberg",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ NUTS3      : Factor w/ 412 levels "DE111","DE112",..: 307 131 40 410 308 364 48 332 83 80 ...
##  $ CodeSTR    : Factor w/ 412 levels "GER_adm2_0001",..: 145 310 219 412 146 379 227 170 262 259 ...
##  $ Code       : num  145 310 219 412 146 379 227 170 262 259 ...
##  $ CodeInt    : int  145 310 219 412 146 379 227 170 262 259 ...
##  $ X6_pop     : num  128286 127788 190143 100960 133329 ...
##  $ X6_nmr     : num  0.118 0.162 -0.153 -0.887 -0.27 ...
##  $ X6_ln_popde: num  2.2 2.21 2.14 2.26 2.31 ...
##  $ X6_z_nmr   : num  0.471 0.572 -0.15 -1.836 -0.419 ...
# Select the columns that we are interested in
GER@data <- GER@data[,c("Name", "X6_nmr", "X6_z_nmr")]

# Rename the columns so there are consistent across countries

names(GER@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(GRE@data)
## 'data.frame':    54 obs. of  7 variables:
##  $ Name       : Factor w/ 54 levels "Achaia","Argolida",..: 9 12 24 46 53 1 10 18 14 17 ...
##  $ Code       : num  27 39 30 41 40 9 1 10 37 26 ...
##  $ X8_3_nmr   : num  0.4264 0.3922 -0.0372 0.2801 -0.0646 ...
##  $ X8_3_pop   : num  93810 137690 134320 107120 108290 ...
##  $ X8_3_ln_pop: num  1.4 1.52 1.84 1.62 1.77 ...
##  $ X8_3_countr: Factor w/ 1 level "Greece": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X8_3_z_nmr : num  0.1164 0.0492 -0.7939 -0.1709 -0.8477 ...
# Select the columns that we are interested in
GRE@data <- GRE@data[,c("Name", "X8_3_nmr", "X8_3_z_nmr")]

# Rename the columns so there are consistent across countries

names(GRE@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(HUN@data)
## 'data.frame':    168 obs. of  14 variables:
##  $ ID_0       : Factor w/ 1 level "103": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ISO        : Factor w/ 1 level "HUN": 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_0     : Factor w/ 1 level "Hungary": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_1       : Factor w/ 20 levels "1","10","11",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_1     : Factor w/ 20 levels "Bács-Kiskun",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_2       : Factor w/ 168 levels "1","10","100",..: 1 81 92 103 114 125 136 147 158 2 ...
##  $ NAME_2     : Factor w/ 168 levels "Abai","Abauj-Hegykozi",..: 6 7 62 65 69 71 72 73 74 82 ...
##  $ TYPE_2     : Factor w/ 1 level "Kistérség": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ENGTYPE_2  : Factor w/ 1 level "Subregion": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X4_2_pop   : num  16631 73426 16114 51354 173236 ...
##  $ X4_2_nmr   : num  -0.34 -0.15 -0.34 -0.43 0.1 -0.17 -0.36 -0.28 -0.14 -0.26 ...
##  $ X4_2_ln_pop: num  1.64 1.79 1.61 1.7 2.07 ...
##  $ X4_2_countr: Factor w/ 1 level "Hungary": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X4_2_z_nmr : num  -0.647 -0.173 -0.647 -0.871 0.45 ...
# Select the columns that we are interested in
HUN@data <- HUN@data[,c("NAME_2", "X4_2_nmr", "X4_2_z_nmr")]

# Rename the columns so there are consistent across countries

names(HUN@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(ITA@data)
## 'data.frame':    107 obs. of  7 variables:
##  $ name       : Factor w/ 107 levels "Agrigento","Alessandria",..: 93 103 58 29 6 2 99 39 86 36 ...
##  $ code       : num  1 2 3 4 5 6 7 8 9 10 ...
##  $ X9_2_pop   : num  2300000 179955 367672 587803 220654 ...
##  $ X9_2_nmr   : num  -0.02319 -0.00389 0.24261 0.17693 0.11194 ...
##  $ X9_2_ln_pop: num  2.53 1.94 2.44 1.93 2.16 ...
##  $ X9_2_countr: Factor w/ 1 level "Italy": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X9_2_z_nmr : num  -0.1826 -0.0932 1.0495 0.745 0.4438 ...
# Select the columns that we are interested in
ITA@data <- ITA@data[,c("name", "X9_2_nmr", "X9_2_z_nmr")]

# Rename the columns so there are consistent across countries

names(ITA@data) <- c("name", "net_migr", "z_score")

In Lithuania probably something is worng with the net migration. It seems identical to z score

# Print the names of the columns
str(LTU@data)
## 'data.frame':    48 obs. of  16 variables:
##  $ ID_0       : Factor w/ 1 level "130": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ISO        : Factor w/ 1 level "LTU": 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_0     : Factor w/ 1 level "Lithuania": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_1       : Factor w/ 10 levels "1","10","2","3",..: 1 1 1 1 1 1 3 3 3 3 ...
##  $ NAME_1     : Factor w/ 10 levels "Alytaus","Kauno",..: 6 6 6 6 6 6 1 1 1 1 ...
##  $ ID_2       : Factor w/ 48 levels "1","10","11",..: 1 12 23 34 44 45 46 47 48 2 ...
##  $ NAME_2     : Factor w/ 48 levels "Akmenes","Alytaus",..: 34 1 9 14 23 29 2 6 18 45 ...
##  $ TYPE_2     : Factor w/ 3 levels "Miesto Savivaldybe",..: 1 2 2 2 2 2 2 3 2 2 ...
##  $ ENGTYPE_2  : Factor w/ 3 levels "City Municipality",..: 1 2 2 2 2 2 2 3 2 2 ...
##  $ NL_NAME_2  : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
##  $ VARNAME_2  : Factor w/ 48 levels "Akmene|Akmene",..: 34 1 9 14 23 29 2 6 18 45 ...
##  $ X6_2v_pop  : Factor w/ 48 levels "17478","18429",..: 37 11 13 25 10 36 16 8 9 12 ...
##  $ X6_2v_nmr  : num  -0.51 -0.86 -0.49 -0.58 -0.74 -0.41 0.52 -0.18 -0.2 -0.32 ...
##  $ X6_2v_ln_po: num  1.43 1.49 1.4 1.33 1.3 1.46 1.33 1.72 1.26 1.09 ...
##  $ X6_2v_count: Factor w/ 1 level "Lithuania": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X6_2v_z_nmr: num  -0.516 -0.913 -0.493 -0.595 -0.776 ...
# Select the columns that we are interested in
LTU@data <- LTU@data[,c("NAME_2", "X6_2v_nmr", "X6_2v_z_nmr")]

# Rename the columns so there are consistent across countries

names(LTU@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(NLD@data)
## 'data.frame':    431 obs. of  13 variables:
##  $ GM_CODE    : Factor w/ 431 levels "GM0003","GM0005",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ GM_NAAM    : Factor w/ 431 levels "'s-Gravenhage",..: 23 31 35 343 85 136 137 148 173 196 ...
##  $ AANT_INW   : Factor w/ 421 levels "100300","10235",..: 37 6 402 375 220 128 39 125 274 139 ...
##  $ OPP_TOT    : Factor w/ 425 levels "10048","1011",..: 159 264 24 268 143 387 398 286 357 326 ...
##  $ OPP_LAND   : Factor w/ 428 levels "10027","10105",..: 159 260 20 265 55 371 400 267 338 323 ...
##  $ OPP_WATER  : Factor w/ 343 levels "-99999998","0",..: 318 221 100 237 335 271 337 260 286 337 ...
##  $ Name       : Factor w/ 431 levels "'s-Gravenhage (gemeente)",..: 23 31 35 343 85 136 137 148 173 196 ...
##  $ Code       : num  8 11 1 21 9 14 15 16 17 18 ...
##  $ X8_pop     : Factor w/ 421 levels "100300","10235",..: 37 6 402 375 220 128 39 125 274 139 ...
##  $ X8_nmr     : num  0.133 0.0766 0.27 0.2424 -1.1639 ...
##  $ X8_ln_popde: num  2.69 2.37 1.92 2.21 2.29 ...
##  $ X8_country : Factor w/ 1 level "Netherlands": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X8_z_nmr   : num  0.388 0.288 0.632 0.583 -1.925 ...
# Select the columns that we are interested in
NLD@data <- NLD@data[,c("Name", "X8_nmr", "X8_z_nmr")]

# Rename the columns so there are consistent across countries

names(NLD@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(NOR@data)
## 'data.frame':    428 obs. of  15 variables:
##  $ OBJTYPE    : Factor w/ 1 level "Kommune": 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAVN       : Factor w/ 422 levels "Agdenes","Alstahaug",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ KOMM       : int  1622 1820 2012 438 719 1871 118 906 220 124 ...
##  $ Shape_Leng : num  79650 126931 610991 152465 60957 ...
##  $ Shape_Area : num  3.19e+08 1.55e+08 3.77e+09 9.47e+08 1.86e+08 ...
##  $ kommnr     : Factor w/ 428 levels "0101","0104",..: 300 350 414 61 120 384 6 145 26 11 ...
##  $ fylke      : Factor w/ 19 levels "01","02","03",..: 15 17 19 4 7 17 1 9 2 1 ...
##  $ Origin     : Factor w/ 428 levels "0101 Halden",..: 300 350 414 61 120 384 6 145 26 11 ...
##  $ Code       : num  300 350 414 61 120 384 6 145 26 11 ...
##  $ Name       : Factor w/ 422 levels "Agdenes","Alstahaug",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ X5_2_pop   : Factor w/ 414 levels "10029","1003",..: 103 369 125 171 321 300 71 272 326 86 ...
##  $ X5_2_nmr   : num  1.042 -1.369 -0.649 -0.779 1.935 ...
##  $ X5_2_ln_pop: num  0.734 1.676 0.719 0.411 1.481 ...
##  $ X5_2_countr: Factor w/ 1 level "Norway": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X5_2_z_nmr : num  1.148 -0.81 -0.225 -0.33 1.874 ...
# Select the columns that we are interested in
NOR@data <- NOR@data[,c("Name", "X5_2_nmr", "X5_2_z_nmr")]

# Rename the columns so there are consistent across countries

names(NOR@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(POL@data)
## 'data.frame':    376 obs. of  16 variables:
##  $ ID_0       : Factor w/ 1 level "181": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ISO        : Factor w/ 1 level "POL": 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_0     : Factor w/ 1 level "Poland": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID_1       : Factor w/ 16 levels "1","10","11",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ NAME_1     : Factor w/ 16 levels "Greater Poland",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ ID_2       : Factor w/ 376 levels "1","10","100",..: 1 112 223 311 322 333 344 355 366 2 ...
##  $ NAME_2     : Factor w/ 376 levels "Aleksandrów",..: 149 148 133 136 153 5 24 130 203 215 ...
##  $ TYPE_2     : Factor w/ 1 level "Powiat": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ENGTYPE_2  : Factor w/ 2 levels "City","County": 2 1 2 2 2 2 2 2 2 2 ...
##  $ NL_NAME_2  : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
##  $ VARNAME_2  : Factor w/ 366 levels "Biala Podlaska",..: 155 26 141 143 158 41 55 138 202 211 ...
##  $ X4_3v_nmr  : num  0.98 -0.21 0.23 -0.22 -0.17 -0.24 0.32 -0.28 -0.22 0.27 ...
##  $ X4_3v_pop  : num  67397 736510 50856 52495 81196 ...
##  $ X4_3v_ln_po: num  2.13 3.4 1.92 1.83 1.91 2.07 1.93 2.06 1.88 2.39 ...
##  $ X4_3v_count: Factor w/ 1 level "Poland": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X4_3v_z_nmr: num  2.508 -0.376 0.691 -0.4 -0.279 ...
# Select the columns that we are interested in
POL@data <- POL@data[,c("NAME_2", "X4_3v_nmr", "X4_3v_z_nmr")]

# Rename the columns so there are consistent across countries

names(POL@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(POR@data)
## 'data.frame':    30 obs. of  11 variables:
##  $ ADMIN_NAME : Factor w/ 30 levels "Alentejo Central",..: 6 7 11 28 23 9 1 3 24 4 ...
##  $ CNTRY_NAME : Factor w/ 1 level "Portugal": 1 1 1 1 1 1 1 1 1 1 ...
##  $ SUBR2001   : int  200 300 161 163 111 189 189 150 166 189 ...
##  $ CNTRY      : int  620 620 620 620 620 620 620 620 620 620 ...
##  $ id         : int  29 30 10 12 1 27 25 9 20 26 ...
##  $ X3_pop     : num  242360 258980 340400 227220 257060 ...
##  $ X3_nmr     : num  -0.0908 -0.2394 0.5758 0.088 -0.4513 ...
##  $ X3_ln_popde: num  2 2.5 2.28 2.11 2.06 ...
##  $ X3_bsu_name: Factor w/ 30 levels "Alentejo Central",..: 28 27 9 26 21 7 4 3 22 1 ...
##  $ X3_country : Factor w/ 1 level "Portugal": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X3_z_nmr   : num  0.2373 0.0528 1.0644 0.4591 -0.2101 ...
# Select the columns that we are interested in
POR@data <- POR@data[,c("ADMIN_NAME", "X3_nmr", "X3_z_nmr")]

# Rename the columns so there are consistent across countries

names(POR@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(ROU@data)
## 'data.frame':    42 obs. of  10 variables:
##  $ NUTS3      : Factor w/ 42 levels "RO111","RO112",..: 14 5 4 17 2 1 15 6 3 16 ...
##  $ Name       : Factor w/ 42 levels "RO111 / Bihor",..: 14 5 4 17 2 1 15 6 3 16 ...
##  $ Code       : num  14 5 4 17 2 1 15 6 3 16 ...
##  $ Area       : num  5032 4431 6307 8542 5340 ...
##  $ X5_3_nmr   : num  0.0394 -0.1146 0.2961 0.1005 0.021 ...
##  $ X5_3_pop   : num  462078 374461 520724 706591 318678 ...
##  $ X5_3_ln_pop: num  1.96 1.93 1.92 1.92 1.78 ...
##  $ X5_3_bsu_na: Factor w/ 42 levels "Alba","Arad",..: 7 34 27 36 6 5 25 33 14 30 ...
##  $ X5_3_countr: Factor w/ 1 level "Romania": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X5_3_z_nmr : num  0.1581 -0.8275 1.8019 0.5493 0.0406 ...
# Select the columns that we are interested in
ROU@data <- ROU@data[,c("Name", "X5_3_nmr", "X5_3_z_nmr")]

# Rename the columns so there are consistent across countries

names(ROU@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(RUS@data)
## 'data.frame':    80 obs. of  8 variables:
##  $ VARNAME_1  : Factor w/ 79 levels "Adygea|Adygeya|Adygheya|Republic of Adygeya|Adygeyskaya A.Obl.|Respublika Adygeya",..: 28 29 30 32 33 34 77 35 36 15 ...
##  $ Name       : Factor w/ 80 levels "Altai Republic",..: 19 20 53 21 22 54 23 24 25 26 ...
##  $ Code       : num  73 34 19 68 75 63 49 20 7 37 ...
##  $ X9_pop     : num  322412 475598 645650 2800000 1300000 ...
##  $ X9_nmr     : num  -0.6 -0.527 -0.224 -0.121 -0.221 ...
##  $ X9_ln_popde: num  -0.151 1.498 0.573 1.462 0.237 ...
##  $ X9_country : Factor w/ 1 level "Russia": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X9_z_nmr   : num  -0.9963 -0.8079 -0.0203 0.2484 -0.0109 ...
# Select the columns that we are interested in
RUS@data <- RUS@data[,c("Name", "X9_nmr", "X9_z_nmr")]

# Rename the columns so there are consistent across countries

names(RUS@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(SPA@data)
## 'data.frame':    52 obs. of  8 variables:
##  $ Name       : Factor w/ 52 levels "Alava","Albacete",..: 37 41 4 12 17 21 24 26 31 43 ...
##  $ Code       : num  35 38 4 11 14 18 21 23 29 41 ...
##  $ Area       : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ X8_2_pop   : num  1100000 981599 675730 1200000 789419 ...
##  $ X8_2_nmr   : num  0.1585 -0.08863 0.08258 -0.06282 0.00659 ...
##  $ X8_2_ln_pop: num  2.41 2.46 1.89 2.22 1.76 ...
##  $ X8_2_countr: Factor w/ 1 level "Spain": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X8_2_z_nmr : num  0.6506 -0.461 0.3091 -0.3449 -0.0327 ...
# Select the columns that we are interested in
SPA@data <- SPA@data[,c("Name", "X8_2_nmr", "X8_2_z_nmr")]

# Rename the columns so there are consistent across countries

names(SPA@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(SWE@data)
## 'data.frame':    290 obs. of  9 variables:
##  $ name       : Factor w/ 290 levels "Ale","Alingss",..: 261 268 220 284 92 34 86 21 195 68 ...
##  $ CodeSTR    : num  114 115 117 120 123 125 126 127 128 136 ...
##  $ Code       : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ area       : num  90.3 359.7 258.6 281.6 50.3 ...
##  $ X1_2_pop   : Factor w/ 290 levels "100029","10040",..: 178 150 175 173 232 130 1 259 85 250 ...
##  $ X1_2_nmr   : num  -0.02472 0.56838 0.20734 0.39857 0.00443 ...
##  $ X1_2_ln_pop: num  2.65 1.93 2.19 2.14 3.13 ...
##  $ X1_2_countr: Factor w/ 1 level "Sweden": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X1_2_z_nmr : num  0.417 1.219 0.73 0.989 0.456 ...
# Select the columns that we are interested in
SWE@data <- SWE@data[,c("name", "X1_2_nmr", "X1_2_z_nmr")]

# Rename the columns so there are consistent across countries

names(SWE@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(TUR@data)
## 'data.frame':    81 obs. of  7 variables:
##  $ Name       : Factor w/ 81 levels "Adana","Adiyaman",..: 1 2 3 4 5 6 8 10 11 12 ...
##  $ Code       : num  1 2 3 4 68 5 6 7 75 8 ...
##  $ X2_2_pop   : num  2100000 594596 701287 553942 379369 ...
##  $ X2_2_nmr   : num  -0.631 -1.391 0.346 -2.731 -0.45 ...
##  $ X2_2_ln_pop: num  2.13 1.9 1.68 1.73 1.7 ...
##  $ X2_2_countr: Factor w/ 1 level "Turkey": 1 1 1 1 1 1 1 1 1 1 ...
##  $ X2_2_z_nmr : num  -0.438 -1.106 0.423 -2.286 -0.278 ...
# Select the columns that we are interested in
TUR@data <- TUR@data[,c("Name", "X2_2_nmr", "X2_2_z_nmr")]

# Rename the columns so there are consistent across countries

names(TUR@data) <- c("name", "net_migr", "z_score")
# Print the names of the columns
str(UK@data)
## 'data.frame':    404 obs. of  12 variables:
##  $ geo_code  : Factor w/ 404 levels "95AA","95BB",..: 397 356 394 195 217 162 10 402 315 183 ...
##  $ geo_label : Factor w/ 404 levels "Aberdeen City",..: 275 129 45 200 278 13 83 231 189 317 ...
##  $ geo_labelw: Factor w/ 22 levels "Abertawe","Blaenau Gwent",..: 14 NA 12 NA NA NA NA 6 NA NA ...
##  $ label     : Factor w/ 404 levels "E92000001E06000001",..: 397 356 394 169 191 136 334 402 289 157 ...
##  $ name      : Factor w/ 404 levels "Aberdeen City",..: 275 129 45 200 278 13 83 231 189 317 ...
##  $ CodeID    : Factor w/ 404 levels "1","10","100",..: 331 286 328 108 133 72 2 338 242 95 ...
##  $ uk_pop    : Factor w/ 401 levels "100000","100031",..: 197 227 107 378 282 57 266 117 267 24 ...
##  $ uk_nmr    : num  0.13 0.04 -0.19 -1.9 -0.46 -0.22 -2.12 0.1 -0.04 0.69 ...
##  $ uk_ln_popd: num  2.74 0.93 2.74 3.42 1.6 2.31 1.91 2.82 3.01 2.98 ...
##  $ uk_bsu_nam: Factor w/ 404 levels "Aberdeen City",..: 275 129 45 200 278 13 335 231 189 317 ...
##  $ uk_country: Factor w/ 1 level "United Kingdom": 1 1 1 1 1 1 1 1 1 1 ...
##  $ uk_z_nmr  : num  0.17 0.02 -0.34 -3.07 -0.78 -0.39 -3.42 0.12 -0.11 1.05 ...
# Select the columns that we are interested in
UK@data <- UK@data[,c("geo_label", "uk_nmr", "uk_z_nmr")]

# Rename the columns so there are consistent across countries

names(UK@data) <- c("name", "net_migr", "z_score")

Combine all the datasets

something was wrong with Norway (NOR) CRS

# Use the coordinate system of Netherlands
# The coordinate system is WGS84
identical_crs <- proj4string(NLD)

# and transform Norway to be compatible
NOR <-spTransform(NOR,CRS(identical_crs))

I downloaded the European countries boundaries (i.e. NUTS 0) from https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/administrative-units-statistical-units/nuts#nuts16 and then selected the countries that are mising from the dataset of net migration

# read in the missing countries boundaries
missing <- readOGR(dsn = "data_missing", 
                layer = "European_countries_modified")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Franciscorowe 1/Dropbox/Francisco/Research/in_progress/EU paper/interactive_map/EU_map/data_missing", layer: "European_countries_modified"
## with 15 features
## It has 5 fields
# Print the names of the columns
str(missing@data)
## 'data.frame':    15 obs. of  5 variables:
##  $ LEVL_CODE: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ NUTS_ID  : Factor w/ 15 levels "AL","CY","FR",..: 1 2 3 4 5 6 8 9 10 12 ...
##  $ CNTR_CODE: Factor w/ 15 levels "AL","CY","FR",..: 1 2 3 4 5 6 8 9 10 12 ...
##  $ NUTS_NAME: Factor w/ 15 levels "FRANCE","HRVATSKA",..: 11 14 1 2 3 4 7 5 15 8 ...
##  $ FID      : Factor w/ 15 levels "AL","CY","FR",..: 1 2 3 4 5 6 8 9 10 12 ...
# create two new columns for net migration and z score to be consistent to the other datasets and fill them with NA values
missing@data$net_migr <- NA
missing@data$z_score <- NA

# Select the columns that we are interested in
missing@data <- missing@data[,c("NUTS_NAME", "net_migr", "z_score")]

# Rename the columns so there are consistent across countries

names(missing@data) <- c("name", "net_migr", "z_score")
# Combine all the datasets into one
combined_data <- rbind(AUT, BEL, BRL, BUL, CHE, CZE, DNK, EST, FIN, GER, GRE, HUN, ITA, LTU, 
                       NLD, NOR, POL, POR, ROU, RUS, SPA, SWE, TUR, UK, missing)

# If we want it as a seperate shapefile
# writeOGR(combined_data , ".", "combined_data", driver="ESRI Shapefile")

summary(combined_data@data)
##           name         net_migr           z_score         
##  Neunkirchen:   2   Min.   :-26.6667   Min.   :-9.884604  
##  Lier       :   2   1st Qu.: -0.4600   1st Qu.:-0.530030  
##  Zwijndrecht:   2   Median : -0.1029   Median :-0.002988  
##  Byala      :   2   Mean   : -0.1419   Mean   : 0.000241  
##  Lom        :   2   3rd Qu.:  0.2612   3rd Qu.: 0.516629  
##  (Other)    :4995   Max.   :  7.7743   Max.   : 8.304665  
##  NA's       :   1   NA's   :27         NA's   :27

Simplify the shapefile as it is too big

library(pryr)
## Registered S3 method overwritten by 'pryr':
##   method      from
##   print.bytes Rcpp
# check the size of the original shapefile
pryr::object_size(combined_data)
## 213 MB
library(rgeos)
## rgeos version: 0.5-2, (SVN revision 621)
##  GEOS runtime version: 3.7.2-CAPI-1.11.2 
##  Linking to sp version: 1.3-1 
##  Polygon checking: TRUE
# Simplify the shapefile but it does not preserve the columns
combined_data_simpl <- gSimplify(combined_data, tol = 0.001, topologyPreserve=TRUE)

# I create a dataframe with the data from the combined data
combined_data_df <- combined_data@data

# Combine spatial polygons and data
combined_data_simpl <- SpatialPolygonsDataFrame(combined_data_simpl, combined_data_df)

# check the size of the simplified shapefile
pryr::object_size(combined_data_simpl)
## 51 MB

Categorise data so to specify colours and labels for the map

# categorise data
combined_data_simpl$z_cat <- cut(combined_data_simpl$z_score,
                   breaks=c(-Inf, -2.0, -0.001, 0.001, 2.0, Inf),
                   labels=c("less than -2", "-2 to 0", "0", "0 to 2", "more than 2"))
library(leaflet)

# Specify the colour palette
pal <- colorFactor(palette = c("red", "salmon1", "white", "steelblue1", "blue"), 
           levels = c("less than -2", "-2 to 0", "0", "0 to 2", "more than 2"), ordered = FALSE)

# pal <- colorBin("RdYlBu", domain = spdf$z_score, bins = bins)

# There is a list of all the available background tiles in
# http://leaflet-extras.github.io/leaflet-providers/preview/index.html
# Create the initial background map, zooming in Europe
colourmap <- leaflet() %>% 
  addTiles() %>% 
  setView(lat = 48, lng = 20, zoom = 4) %>% 
  addProviderTiles(providers$NASAGIBS.ViirsEarthAtNight2012, group = "NASAGIBS.ViirsEarthAtNight2012") %>% 
  addProviderTiles(providers$Stamen.TonerLite, group = "Stamen.TonerLite")

# Create the interactive map showing the sequence clusters
interactive_map <- colourmap %>%
  addPolygons(data = combined_data_simpl,
              fillColor = ~pal(z_cat),
              weight = 0.4,
              opacity = 0.8,
              color = "black",
              dashArray = "3",
              fillOpacity = 0.7,
              popup = paste("Name: ", combined_data_simpl$name, "<br>",
                            "Net Migration: ", combined_data_simpl$net_migr, "<br>"),
              group = "Net migration in Europe",
              highlight = highlightOptions(
                weight = 5,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE)) %>% 
  addLegend(pal = pal,
            values  = combined_data_simpl$z_cat,
            na.label = "Missing data",
            position = "bottomleft",
            title = "Net migration in Europe (z-scores)") %>% 
  addLayersControl(baseGroups = c("NASAGIBS.ViirsEarthAtNight2012", "Stamen.TonerLite"),
    overlayGroups = "Net migration in Europe",
    options = layersControlOptions(collapsed = FALSE))


interactive_map

If we want to save only the map

library(htmlwidgets)
saveWidget(interactive_map, file="map.html", title = "Net Migration in Europe", selfcontained=TRUE)
# shhh <- readOGR(dsn = dirname(ff[1]), layer = basename(ff[1])) 
# 
# shhhdddd <- readOGR(dsn = "data_to_use", 
#                 layer = tools::file_path_sans_ext(basename(ff[1])))
# 
# shhhdddd@data
# 
# 
# basename(ff[1])
# dirname(ff[1])
# 
# getwd()
# 
# tools::file_path_sans_ext(basename(ff[1]))
# paste0("../data_to_use/",basename(ff[1]))